module MC.View {
	export class ComDesign {
		public constructor() {
			this.init();
		}
		private init() {
			MC.apiD("/api/member/company/com-design-list", {}, (json: any) => {
				console.log(json);
				for (let i = 0; i < json.length; i++) {
					this.addli(json[i]);
				}
			});
			$('.addlibtn').on('click', () => {
				let obj = { 
					name: $('#name').val(),
					num: $('#num').val(), 
				}; 
				if (obj.name == '') {
					MC.msg.alert('新加产品名称不能为空');
					return;
				}
				if (obj.num == '') {
					MC.msg.alert('新加男装不能为空');
					return;
				} 
				this.addli(obj); 
				$('#name').val("");
				$('#num').val(""); 
			});

			$('#designlist').on('click', '.goodsPanel .delete.button', function () {
				let obj = $(this).parents('.goodsPanel');
				MC.msg.confirm('确认删除本行?', () => {
					obj.remove();
				});
			});

			$('.savebtn').on('click', () => {
				let list = $('#designlist .goodsPanel');
				let array = [];
				for (let i = 0; i < list.length; i++) {
					let one = $(list[i]);
					let obj = { 
						name: one.find('.name').text(),
						num: one.find('.num').val()
					};
					array.push(obj);
				}
				console.log(array);
				MC.apiD("/api/member/company/com-design-set", { designs:
						JSON.stringify(array) }, () => {
					MC.msg.show('保存成功');
				});
			});

		}
		private addli(obj: any) {
			//	console.log(obj);
			let li = ` <div class="goodsPanel">
        <div class="goodsNameStyleLast name">`+ obj.name + `</div>
        <div class="goodsRightStyleLast">
            <input type="number" class="regCodeCenter num"  value="`+ obj.num + `">
        </div>
		<a class="button delete">删除</a>
    </div>`;
			$('#designlist').append(li);
		}
	}
}